home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / Interfaces&Libraries / Universal / Interfaces / PInterfaces / DigitalSignature.p < prev    next >
Encoding:
Text File  |  1998-02-12  |  16.5 KB  |  387 lines  |  [TEXT/MPS ]

  1. {
  2.      File:        DigitalSignature.p
  3.  
  4.      Contains:    Digital Signature Interfaces.
  5.  
  6.      Version:    Technology:    AOCE toolbox 1.02
  7.                  Release:    Universal Interfaces 3.1
  8.  
  9.      Copyright:    © 1994-1998 by Apple Computer, Inc., all rights reserved.
  10.  
  11.      Bugs?:        Please include the the file and version information (from above) with
  12.                  the problem description.  Developers belonging to one of the Apple
  13.                  developer programs can submit bug reports to:
  14.  
  15.                      devsupport@apple.com
  16.  
  17. }
  18. {$IFC UNDEFINED UsingIncludes}
  19. {$SETC UsingIncludes := 0}
  20. {$ENDC}
  21.  
  22. {$IFC NOT UsingIncludes}
  23.  UNIT DigitalSignature;
  24.  INTERFACE
  25. {$ENDC}
  26.  
  27. {$IFC UNDEFINED __DIGITALSIGNATURE__}
  28. {$SETC __DIGITALSIGNATURE__ := 1}
  29.  
  30. {$I+}
  31. {$SETC DigitalSignatureIncludes := UsingIncludes}
  32. {$SETC UsingIncludes := 1}
  33.  
  34. {$IFC UNDEFINED __MACTYPES__}
  35. {$I MacTypes.p}
  36. {$ENDC}
  37. {$IFC UNDEFINED __MIXEDMODE__}
  38. {$I MixedMode.p}
  39. {$ENDC}
  40. {$IFC UNDEFINED __FILES__}
  41. {$I Files.p}
  42. {$ENDC}
  43.  
  44.  
  45. {$PUSH}
  46. {$ALIGN MAC68K}
  47. {$LibExport+}
  48.  
  49. { values of SIGNameAttributeType }
  50.  
  51. CONST
  52.     kSIGCountryCode                = 0;
  53.     kSIGOrganization            = 1;
  54.     kSIGStreetAddress            = 2;
  55.     kSIGState                    = 3;
  56.     kSIGLocality                = 4;
  57.     kSIGCommonName                = 5;
  58.     kSIGTitle                    = 6;
  59.     kSIGOrganizationUnit        = 7;
  60.     kSIGPostalCode                = 8;
  61.  
  62.  
  63. TYPE
  64.     SIGNameAttributeType                = UInt16;
  65. Certificate status codes returned in SIGCertInfo or SIGSignerInfo from
  66. either SIGGetCertInfo or SIGGetSignerInfo respectively. kSIGValid means that
  67. the certificate is currently valid. kSIGPending means the certificate is
  68. currently not valid - but will be.  kSIGExpired means the certificate has
  69. expired. A time is always associated with a SIGCertStatus.  In each case the
  70. time has a specific interpretation.  When the status is kSIGValid the time is
  71. when the certificate will expire. When the status is kSIGPending the time is
  72. when the certificate will become valid. When the status is kSIGExpired the time
  73. is when the certificate expired. In the SIGCertInfo structure, the startDate
  74. and endDate fields hold the appropriate date information.  In the SIGSignerInfo
  75. structure, this information is provided in the certSetStatusTime field. In the
  76. SIGSignerInfo struct, the status time is actually represented by the SIGSignatureStatus
  77. field which can contain any of the types below. NOTE: The only time you will get 
  78. a kSIGInvalid status is when it pertains to a SIGSignatureStatus field and only when
  79. you get a signature that was created after the certificates expiration date, something
  80. we are not allowing on the Mac but that may not be restricted on other platforms. Also, 
  81. it will not be possible to get a kSIGPending value for SIGSignatureStatus on the Mac but
  82. possibly allowed by other platforms.
  83. }
  84. { Values for SIGCertStatus or SIGSignatureStatus }
  85.  
  86. CONST
  87.     kSIGValid                    = 0;                            {  possible for either a SIGCertStatus or SIGSignatureStatus  }
  88.     kSIGPending                    = 1;                            {  possible for either a SIGCertStatus or SIGSignatureStatus  }
  89.     kSIGExpired                    = 2;                            {  possible for either a SIGCertStatus or SIGSignatureStatus * possible only for a SIGSignatureStatus  }
  90.     kSIGInvalid                    = 3;
  91.  
  92.  
  93. TYPE
  94.     SIGCertStatus                        = UInt16;
  95.     SIGSignatureStatus                    = UInt16;
  96. { Number of bytes needed for a digest record when using SIGDigest }
  97.  
  98. CONST
  99.     kSIGDigestSize                = 16;
  100.  
  101.  
  102. TYPE
  103.     SIGDigestData                        = PACKED ARRAY [0..15] OF Byte;
  104.     SIGDigestDataPtr                    = ^Byte;
  105.     SIGCertInfoPtr = ^SIGCertInfo;
  106.     SIGCertInfo = RECORD
  107.         startDate:                UInt32;                                    {  cert start validity date  }
  108.         endDate:                UInt32;                                    {  cert end validity date  }
  109.         certStatus:                SIGCertStatus;                            {  see comment on SIGCertStatus for definition  }
  110.         certAttributeCount:        UInt32;                                    {  number of name attributes in this cert  }
  111.         issuerAttributeCount:    UInt32;                                    {  number of name attributes in this certs issuer  }
  112.         serialNumber:            Str255;                                    {  cert serial number  }
  113.     END;
  114.  
  115.     SIGSignerInfoPtr = ^SIGSignerInfo;
  116.     SIGSignerInfo = RECORD
  117.         signingTime:            UInt32;                                    {  time of signing  }
  118.         certCount:                UInt32;                                    {  number of certificates in the cert set  }
  119.         certSetStatusTime:        UInt32;                                    {  Worst cert status time. See comment on SIGCertStatus for definition  }
  120.         signatureStatus:        SIGSignatureStatus;                        {  The status of the signature. See comment on SIGCertStatus for definition }
  121.     END;
  122.  
  123.     SIGNameAttributesInfoPtr = ^SIGNameAttributesInfo;
  124.     SIGNameAttributesInfo = RECORD
  125.         onNewLevel:                BOOLEAN;
  126.         filler1:                BOOLEAN;
  127.         attributeType:            SIGNameAttributeType;
  128.         attributeScript:        ScriptCode;
  129.         attribute:                Str255;
  130.     END;
  131.  
  132.     SIGContextPtr                        = Ptr;
  133.     SIGSignaturePtr                        = Ptr;
  134. {
  135. Certificates are always in order. That is, the signers cert is always 0, the
  136. issuer of the signers cert is always 1 etc… to the number of certificates-1.
  137. You can use this constant for readability in your code.
  138. }
  139.  
  140. CONST
  141.     kSIGSignerCertIndex            = 0;
  142.  
  143. {
  144. Call back procedure supplied by developer, return false to cancel the current
  145. process.
  146. }
  147.  
  148. TYPE
  149. {$IFC TYPED_FUNCTION_POINTERS}
  150.     SIGStatusProcPtr = FUNCTION: BOOLEAN;
  151. {$ELSEC}
  152.     SIGStatusProcPtr = ProcPtr;
  153. {$ENDC}
  154.  
  155.     SIGStatusUPP = UniversalProcPtr;
  156.  
  157. CONST
  158.     uppSIGStatusProcInfo = $00000010;
  159.  
  160. FUNCTION NewSIGStatusProc(userRoutine: SIGStatusProcPtr): SIGStatusUPP;
  161.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  162.     INLINE $2E9F;
  163.     {$ENDC}
  164.  
  165. FUNCTION CallSIGStatusProc(userRoutine: SIGStatusUPP): BOOLEAN;
  166.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  167.     INLINE $205F, $4E90;
  168.     {$ENDC}
  169. {
  170. Resource id's of standard signature icon suite, all sizes and colors are available.
  171. }
  172.  
  173. CONST
  174.     kSIGSignatureIconResID        = -16797;
  175.     kSIGValidSignatureIconResID    = -16799;
  176.     kSIGInvalidSignatureIconResID = -16798;
  177.  
  178. { ——————————————————————————————— CONTEXT CALLS ——————————————————————————————— 
  179. To use the Digital Signature toolbox you will need a SIGContextPtr.  To create
  180. a SIGContextPtr you simply call SIGNewContext and it will create and initialize
  181. a context for you.  To free the memory occupied by the context and invalidate
  182. its internal data, call SIGDisposeContext. An initialized context has no notion
  183. of the type of operation it will be performing however, once you call
  184. SIGSignPrepare SIGVerifyPrepare, or SIGDigestPrepare, the contexts operation
  185. type is set and to switch  to another type of operation will require creating a
  186. new context. Be sure to pass the same context to corresponding toolbox calls
  187. (ie SIGSignPrepare, SIGProcessData, SIGSign)  in other words mixing lets say
  188. signing and verify calls with the same context is not allowed.
  189. }
  190. FUNCTION SIGNewContext(VAR context: SIGContextPtr): OSErr;
  191.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  192.     INLINE $203C, $0002, $076C, $AA5D;
  193.     {$ENDC}
  194. FUNCTION SIGDisposeContext(context: SIGContextPtr): OSErr;
  195.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  196.     INLINE $203C, $0002, $076D, $AA5D;
  197.     {$ENDC}
  198.  
  199. { ——————————————————————————————— SIGNING CALLS ——————————————————————————————— 
  200. Once you have created a SIGContextPtr, you create a signature by calling
  201. SIGSignPrepare once, followed by n calls to SIGProcessData, followed by one call
  202. toRcpt SIGSign. To create another signature on different data but for the same
  203. signer, don't dispose of the context and call SIGProcessData for the new data
  204. followed by a call SIGSign again. In this case the signer will not be prompted
  205. for their signer and password again as it was already provided.  Once you call
  206. SIGDisposeContext, all signer information will be cleared out of the context and
  207. the signer will be re-prompted.  The signer file FSSpecPtr should be set to nil
  208. if you want the toolbox to use the last signer by default or prompt for a signer
  209. if none exists.  The prompt parameter can be used to pass a string to be displayed
  210. in the dialog that prompts the user for their password.  If the substring "^1"
  211. (without the quotes) is in the prompt string, then the toolbox will replace it
  212. with the name of the signer from the signer selected by the user.  If an empty
  213. string is passed, the following default string will be sent to the toolbox
  214. "\pSigning as ^1.".  You can call any of the utility routines after SIGSignPrepare
  215. or SIGSign to get information about the signer or certs.
  216. }
  217. FUNCTION SIGSignPrepare(context: SIGContextPtr; {CONST}VAR signerFile: FSSpec; prompt: Str255; VAR signatureSize: Size): OSErr;
  218.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  219.     INLINE $203C, $0008, $076E, $AA5D;
  220.     {$ENDC}
  221. FUNCTION SIGSign(context: SIGContextPtr; signature: SIGSignaturePtr; statusProc: SIGStatusProcPtr): OSErr;
  222.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  223.     INLINE $203C, $0006, $076F, $AA5D;
  224.     {$ENDC}
  225.  
  226. { ——————————————————————————————— VERIFYING CALLS ——————————————————————————————— 
  227. Once you have created a SIGContextPtr, you verify a signature by calling
  228. SIGVerifyPrepare  once, followed by n calls to SIGProcessData, followed by one
  229. call to SIGVerify. Check the return code from SIGVerify to see if the signature
  230. verified or not (noErr is returned on  success otherwise the appropriate error
  231. code).  Upon successfull verification, you can call any of the utility routines
  232. toRcpt find out who signed the data.
  233. }
  234. FUNCTION SIGVerifyPrepare(context: SIGContextPtr; signature: SIGSignaturePtr; signatureSize: Size; statusProc: SIGStatusProcPtr): OSErr;
  235.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  236.     INLINE $203C, $0008, $0770, $AA5D;
  237.     {$ENDC}
  238. FUNCTION SIGVerify(context: SIGContextPtr): OSErr;
  239.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  240.     INLINE $203C, $0002, $0771, $AA5D;
  241.     {$ENDC}
  242. { —————————————————————————————— DIGESTING CALLS —————————————————————————————— 
  243. Once you have created a SIGContextPtr, you create a digest by calling
  244. SIGDigestPrepare once, followed by n calls to SIGProcessData, followed by one
  245. call to SIGDigest.  You can dispose of the context after SIGDigest as the
  246. SIGDigestData does not reference back into it.  SIGDigest returns the digest in
  247. digest.
  248. }
  249. FUNCTION SIGDigestPrepare(context: SIGContextPtr): OSErr;
  250.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  251.     INLINE $203C, $0002, $0772, $AA5D;
  252.     {$ENDC}
  253. FUNCTION SIGDigest(context: SIGContextPtr; VAR digest: SIGDigestData): OSErr;
  254.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  255.     INLINE $203C, $0004, $0773, $AA5D;
  256.     {$ENDC}
  257.  
  258. { —————————————————————————————— PROCESSING DATA —————————————————————————————— 
  259. To process data during a digest, sign, or verify operation call SIGProcessData
  260. as many times as necessary and with any sized blocks of data.  The data needs to
  261. be processed in the same order during corresponding sign and verify operations
  262. but does not need to be processed in the same sized chunks (i.e., the toolbox
  263. just sees it as a continuous bit stream).
  264. }
  265. FUNCTION SIGProcessData(context: SIGContextPtr; data: UNIV Ptr; dataSize: Size): OSErr;
  266.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  267.     INLINE $203C, $0006, $0774, $AA5D;
  268.     {$ENDC}
  269.  
  270. { ——————————————————————————————— UTILITY CALLS ——————————————————————————————— 
  271. Given a context that has successfully performed a verification SIGShowSigner
  272. will  display a modal dialog with the entire distinguished name of the person
  273. who signed the data. the prompt (if supplied) will appear at the top of the
  274. dialog.  If no prompt is specified, the default prompt "\pVerification
  275. Successfull." will appear.
  276.  
  277. Given a context that has been populated by calling SIGSignPrepare, SIGSign or a
  278. successful SIGVerify, you can make the remaining utility calls:
  279.  
  280. SIGGetSignerInfo will return the SignerInfo record.  The certCount can be used
  281. toRcpt index into the certificate set when calling SIGGetCertInfo,
  282. SIGGetCertNameAttributes or SIGGetCertIssuerNameAttributes. The signingTime is
  283. only defined if the call is made after SIGSign  or SIGVerify. The certSetStatus
  284. will tell you the best status of the entire certificate set while
  285. certSetStatusTime will correspond to the time associated with that status (see
  286. definitions above).
  287.  
  288. SIGGetCertInfo will return the SIGCertInfo record when given a valid index into
  289. the cert set in  certIndex.  Note: The cert at index kSIGSignerCertIndex is
  290. always the signers certificate.  The  serial number, start date and end date
  291. are there should you wish to display that info.  The  certAttributeCount and
  292. issuerAttributeCount provide the number of parts in the name of that certificate
  293. or that certificates issuer respectively.  You use these numbers to index into
  294. either SIGGetCertNameAttributes or SIGGetCertIssuerNameAttributes to retrieve
  295. the name. The certStatus will tell you the status of the certificate while
  296. certStatusTime will correspond to the time associated with that status (see
  297. definitions above).
  298.  
  299. SIGGetCertNameAttributes and SIGGetCertIssuerNameAttributes return name parts
  300. of the certificate at  certIndex and attributeIndex.  The newLevel return value
  301. tells you wether the name attribute returned is at the same level in the name
  302. hierarchy as the previous attribute.  The type return value tells you  the type
  303. of attribute returned. nameAttribute is the actual string containing the name
  304. attribute.   So, if you wanted to display the entire distinguished name of the
  305. person who's signature was just validated you could do something like this;
  306.  
  307.     (…… variable declarations and verification code would preceed this sample ……)
  308.  
  309.     error = SIGGetCertInfo(verifyContext, kSIGSignerCertIndex, &certInfo);
  310.     HandleErr(error);
  311.  
  312.     for (i = 0; i <= certInfo.certAttributeCount-1; i++)
  313.         (
  314.         error = SIGGetCertNameAttributes(
  315.             verifyContext, kSIGSignerCertIndex, i, &newLevel, &type, theAttribute);
  316.         HandleErr(error);
  317.         DisplayNamePart(theAttribute, type, newLevel);
  318.         )
  319. }
  320. FUNCTION SIGShowSigner(context: SIGContextPtr; prompt: Str255): OSErr;
  321.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  322.     INLINE $203C, $0004, $0775, $AA5D;
  323.     {$ENDC}
  324. FUNCTION SIGGetSignerInfo(context: SIGContextPtr; VAR signerInfo: SIGSignerInfo): OSErr;
  325.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  326.     INLINE $203C, $0004, $0776, $AA5D;
  327.     {$ENDC}
  328. FUNCTION SIGGetCertInfo(context: SIGContextPtr; certIndex: UInt32; VAR certInfo: SIGCertInfo): OSErr;
  329.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  330.     INLINE $203C, $0006, $0777, $AA5D;
  331.     {$ENDC}
  332. FUNCTION SIGGetCertNameAttributes(context: SIGContextPtr; certIndex: UInt32; attributeIndex: UInt32; VAR attributeInfo: SIGNameAttributesInfo): OSErr;
  333.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  334.     INLINE $203C, $0008, $0778, $AA5D;
  335.     {$ENDC}
  336. FUNCTION SIGGetCertIssuerNameAttributes(context: SIGContextPtr; certIndex: UInt32; attributeIndex: UInt32; VAR attributeInfo: SIGNameAttributesInfo): OSErr;
  337.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  338.     INLINE $203C, $0008, $0779, $AA5D;
  339.     {$ENDC}
  340.  
  341.  
  342. { ——————————————————————————— FILE SIGN & VERIFY CALLS —————————————————————————— 
  343. These calls allow you to detect the presence of a standard signtaure in a file as 
  344. well as sign and verify files in a standard way.  An example of this is the Finder, 
  345. which uses these calls to allow the user to "drop sign" a file.
  346.  
  347. To detect if a file is signed in the standard way, pass the FSSpec of the file to SIGFileIsSigned.
  348. A result of noErr means the file is in fact signed, otherwise, a kSIGNoSignature error will
  349. be returned.
  350.  
  351. Once you have created a SIGContextPtr, you can make calls to either sign or verify a file in
  352. a standard way: 
  353.  
  354. To sign a file, call SIGSignPrepare followed by 'n' number of calls to SIGSignFile,
  355. passing it the file spec for each file you wish to sign in turn.  You supply the context, the signature 
  356. size that was returned from SIGSignPrepare and an optional call back proc.  The call will take care of all
  357. the processing of data and affixing the signature to the file. If a signature already exists in the file, 
  358. it is replaced with the newly created signature.
  359.  
  360. To verify a file that was signed using SIGSignFile, call SIGVerifyFile passing it a new context and 
  361. the file spec.  Once this call has completed, if the verification is successfull, you can pass the context 
  362. to SIGShowSigner to display the name of the person who signed the file.
  363. }
  364. FUNCTION SIGFileIsSigned({CONST}VAR fileSpec: FSSpec): OSErr;
  365.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  366.     INLINE $203C, $0002, $09C4, $AA5D;
  367.     {$ENDC}
  368. FUNCTION SIGSignFile(context: SIGContextPtr; signatureSize: Size; {CONST}VAR fileSpec: FSSpec; statusProc: SIGStatusProcPtr): OSErr;
  369.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  370.     INLINE $203C, $0008, $09C5, $AA5D;
  371.     {$ENDC}
  372. FUNCTION SIGVerifyFile(context: SIGContextPtr; {CONST}VAR fileSpec: FSSpec; statusProc: SIGStatusProcPtr): OSErr;
  373.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  374.     INLINE $203C, $0006, $09C6, $AA5D;
  375.     {$ENDC}
  376. {$ALIGN RESET}
  377. {$POP}
  378.  
  379. {$SETC UsingIncludes := DigitalSignatureIncludes}
  380.  
  381. {$ENDC} {__DIGITALSIGNATURE__}
  382.  
  383. {$IFC NOT UsingIncludes}
  384.  END.
  385. {$ENDC}
  386.